package com.lheia.tool

import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import java.util.regex.Matcher
import java.util.regex.Pattern

class ToolString {
    static final String UTF_8 = "UTF-8"
    static final String ISO = "ISO-8859-1"
    static final String GBK = "GBK"
    /**
     * URL 解码
     *
     * @return String
     * @author lifq
     * @date 2015-3-17 下午04:09:51
     */
     static String getURLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * URL 转码
     *
     * @return String
     * @author lifq
     * @date 2015-3-17 下午04:10:28
     */
     static String getURLEncoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLEncoder.encode(str, UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    static def main(def args) {
        Document document = Jsoup.connect("http://new.123jcb.com/jcb-portal/criterion/criteriaDetail?id=3775").get();
        println(document)
    }


    /**
     * 首字母转小写
     * @param str
     * @return
     */
    static String toLowerCaseFirstOne(String str){
        if(Character.isLowerCase(str.charAt(0)))
            return str
        else
            return (new StringBuilder()).append(Character.toLowerCase(str.charAt(0))).append(str.substring(1)).toString();
    }
    /**
     * 首字母转大写
     * @param str
     * @return
     */
    static String toUpperCaseFirstOne(String str){
        if(Character.isUpperCase(str.charAt(0)))
            return str
        else
            return (new StringBuilder()).append(Character.toUpperCase(str.charAt(0))).append(str.substring(1)).toString();
    }
    /**
     * 替换模板
     * @param tempText
     * @param property
     * @return
     */
    static String replaceContent(String temp,Map property,reg="#"){
        def tempText = new String(temp)
        /**替换简单字符串**/
        int startStrIndex = tempText.indexOf("!${reg}-")
        while(startStrIndex !=-1){
            int endStrIndex = tempText.indexOf("-${reg}!")
            String strReplaceKey = tempText.substring(startStrIndex+reg.length()+2,endStrIndex)
            println("替换关键字:"+strReplaceKey)
            tempText = tempText.replaceAll("!${reg}-${strReplaceKey}-${reg}!",property[strReplaceKey])
            startStrIndex=tempText.indexOf("!${reg}-")
        }
        return tempText
    }

    /**
     * 替换字符串循环
     * !loop-aa!each-bbb-each!aa-loop!
     *
     * @param tempText
     * @param property
     * @return
     */
    static String replaceLoopContent(String tempText,Map propertyList){
        Set loopKey = matchList(tempText,/(?<=!loop-)\w+(?=-)/)
        loopKey.each {
            String startTag = "!loop-"+it+"-"
            String endTag = "-loop-"+it+"!"
            println("loop关键字"+it)
            String subTempText = tempText.substring(tempText.indexOf(startTag)+startTag.length(),tempText.indexOf(endTag))
            StringBuilder subString = new StringBuilder()
            /**
             *取出loopkey对应的list
             *  **/
            propertyList.get(it).each {
                subString.append(replaceContent(subTempText,it,"each")+"\n")
            }
            tempText=tempText.replace(startTag+subTempText+endTag,subString)
        }
        return tempText
    }
    /**
     * 返回正则匹配列表
     * @param str
     * @param regex
     * @return
     */
    static List<String> matchList(String str,String regex){
        def list = []
        Matcher matcher= (~regex).matcher(str)
        while(matcher.find()){
            list << (str.substring(matcher.start(),matcher.end()))
        }
        return list.unique()
    }

    static String replaceTemp(String tempText,Map propertyList){
        tempText =  replaceContent(tempText,propertyList)
        tempText =  replaceLoopContent(tempText,propertyList)
        return tempText
    }
    static Set removeNullAndEmpty(List list) {
        def set = new HashSet()
        set.addAll(list)
        set.remove("")
        set.remove(null)
        return set
    }

    private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
    private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
    private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
    private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
    static final String regEx_year = "[1-2]{1}[0,9]{1}[0,1,9]{1}[0-9]{1}"
    static final String regEx_date = "[1-2]{1}[0,9]{1}[0,1,2,9]{1}[0-9]{1}[年|\\-|/]{1}[0,1]?[0-9]{1}[月|\\-|/]{1}[0-3]?[0-9]{1}[日|\\-|/]?"
    /**
     * @param htmlStr
     * @return
     *  删除Html标签
     */
     static String delHTMLTag(String htmlStr) {
        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); // 过滤script标签

        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); // 过滤style标签

        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); // 过滤html标签

        Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
        Matcher m_space = p_space.matcher(htmlStr);
        htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
        return htmlStr.trim(); // 返回文本字符串
    }
     static String getTextFromHtml(String htmlStr){
        htmlStr = delHTMLTag(htmlStr);
        htmlStr = htmlStr.replaceAll("&nbsp;", "");
//        htmlStr = htmlStr.substring(0, htmlStr.indexOf("。")+1);
        return htmlStr;
    }
     static String getMatcher(String regex, String source) {
        String result = "";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(source);
        while (matcher.find()) {
            result = matcher.group(1);
        }
        return result;
    }
}
